<?php
/*
 * CakeMap -- a google maps integrated application built on CakePHP framework.
 * Copyright (c) 2005 Garrett J. Woodworth : gwoo@rd11.com
 * rd11,inc : http://rd11.com
 *
 * @author      gwoo <gwoo@rd11.com>
 * @version     0.10.1311_pre_beta
 * @license     OPPL
 *
 */
class GoogleHelper extends Helper {

	function map($default, $data, $style = 'width: 500px; height: 400px' )
	{
		//if (empty($default)){return "error: You have not specified an address to map"; exit();}
		$markerData = '';
		if(is_array($data)){
			foreach ($data as $key => $map){

				if(!empty($map['Company']['latitude']) 
						&& !empty($map['Company']['longitude'])){
					$map['Company']['catch_comment'] = str_replace("\r\n","<Br/>",$map['Company']['catch_comment']);
					$map['Company']['icon'] = 'G_DEFAULT_ICON';
						
					$markerData .= "markers[{$key}] = createObject('{$map['Company']['id']}','{$map['Company']['comp_name_kanji']}','{$map['Company']['catch_comment']}',{$map['Company']['latitude']},{$map['Company']['longitude']},'{$map['Company']['address1']}',{$map['Company']['icon']}); \r\n";
				}
				
				if(!empty($map['VCompany']['latitude']) 
						&& !empty($map['VCompany']['longitude'])){
						
					$map['VCompany']['catch_comment'] = str_replace("\r\n","<Br/>",$map['VCompany']['catch_comment']);
					if($map['VCompany']['total_cnt']==0 && $map['VCompany']['office_type_group_id']==1){
						$map['VCompany']['icon'] = 'hsp01Icon';}
					elseif($map['VCompany']['total_cnt']>0 && $map['VCompany']['office_type_group_id']==1){
						$map['VCompany']['icon'] = 'hsp02Icon';}
					elseif($map['VCompany']['total_cnt']==0 && $map['VCompany']['office_type_group_id']==2){
						$map['VCompany']['icon'] = 'drg01Icon';}
					elseif($map['VCompany']['total_cnt']>0 && $map['VCompany']['office_type_group_id']==2){
						$map['VCompany']['icon'] = 'drg02Icon';}
					elseif($map['VCompany']['total_cnt']==0 && $map['VCompany']['office_type_group_id']==3){
						$map['VCompany']['icon'] = 'com01Icon';}
					elseif($map['VCompany']['total_cnt']>0 && $map['VCompany']['office_type_group_id']==3){
						$map['VCompany']['icon'] = 'com02Icon';}
					else{
						$map['VCompany']['icon'] = 'G_DEFAULT_ICON';
					}
						
					$markerData .= "markers[{$key}] = createObject('{$map['VCompany']['id']}','{$map['VCompany']['comp_name_kanji']}','{$map['VCompany']['catch_comment']}',{$map['VCompany']['latitude']},{$map['VCompany']['longitude']},'求人広告(2{$map['VCompany']['total_cnt']}件)',{$map['VCompany']['icon']}); \r\n";
				}
						
			}
		}
		
		$out = "<div id=\"map\"";
		$out .= isset($style) ? " style=\"".$style."\"" : null;
		$out .= " ></div>";
		$out .= "
		<div id=\"message\"></div>
	    <script type=\"text/javascript\">
	    //<![CDATA[

	    var draggableflg = true;
	    var Markerable = true;

	    var markers = new Array();
	    
	    var gLocalSearch;
	    var gMap;
	    var gSelectedResults = [];
	    var gCurrentResults = [];

	    
	    function createObject(id,title,address,latitude,longitude,description,icon){
	    	var obj = new Object();
	    	obj.id = id;
	    	obj.title = title;
	    	obj.address = address;
	    	obj.latitude = latitude;
	    	obj.longitude = longitude;
	    	obj.description = description;
	    	obj.icon = icon;
	    	return obj;
	    }
	    
		function createIcon(iconName){		
			var icon = new GIcon();
			icon.image = \"/img/map/icon_\"+iconName+\".png\";
			icon.shadow = \"http://yak/img/map/mm_20_shadow.png\";
			icon.iconSize = new GSize(28, 40);
			icon.shadowSize = new GSize(52, 40);
			icon.iconAnchor = new GPoint(14, 40);
			icon.infoWindowAnchor = new GPoint(13, 12);
			return icon;
		}
		
		function createInnerHtml(marker){
			var innerHtml = '';
			for (var i = 0; i < markers.length; i++) {
				if(markers[i].marker==marker){
					innerHtml = '<a href=\'/companies/showDetail/' + markers[i].id + '\'>' + markers[i].title + '</a><br/>';
					innerHtml += markers[i].address+'<br/>';
					innerHtml += markers[i].description+'<br/>';
				}
			}
			return innerHtml;
		}	

		function createMarker(point,icon) {
		  
			var marker = new GMarker(point,{draggable: draggableflg, icon: icon});
			if(document.getElementById(\"latitude\")) document.getElementById(\"latitude\").value = marker.getPoint().y;
			if(document.getElementById(\"longitude\")) document.getElementById(\"longitude\").value = marker.getPoint().x;

			GEvent.addListener(marker, \"dragstart\", function() {
			  gMap.closeInfoWindow();
			  });

			GEvent.addListener(marker, \"dragend\", function() {
		      marker.openInfoWindowHtml('正確な御社の位置を設定して下さい。<Br/>（地図上のマークをマウスで移動して設定します）');
			  if(document.getElementById(\"latitude\")) document.getElementById(\"latitude\").value = marker.getPoint().y;
			  if(document.getElementById(\"longitude\")) document.getElementById(\"longitude\").value = marker.getPoint().x;
			  });

		    return marker;
		}
		
		function localSearch(){
			if(document.getElementById(\"q\")){
		        var input = document.getElementById(\"q\");
		        
		        // Execute the initial search
	      	    gLocalSearch.execute(input.value);
			}
		}

	    // Called when Local Search results are returned, we clear the old
	    // results and load the new ones.
	    function OnLocalSearch() {
	      if (!gLocalSearch.results) return;
	
	      // move the map to the first result
	      var first = gLocalSearch.results[0];
	      if(markers[0]){
			var point = new GLatLng(first.lat,first.lng);
			markers[0].marker.setPoint(point);
			markers[0].marker.openInfoWindowHtml('正確な御社の位置を設定して下さい。<Br/>（地図上のマークをマウスで移動して設定します');
			gMap.setZoom(15);
			if(document.getElementById(\"latitude\")) document.getElementById(\"latitude\").value = markers[0].marker.getPoint().y;
			if(document.getElementById(\"longitude\")) document.getElementById(\"longitude\").value = markers[0].marker.getPoint().x;
	      }else{
		  	if(Markerable){
				var point = new GLatLng(first.lat,first.lng);
		  		markers[0].marker = createMarker(point,G_DEFAULT_ICON);
				gMap.addOverlay(markers[0]);
				markers[0].marker.openInfoWindowHtml('正確な御社の位置を設定して下さい。<Br/>（地図上のマークをマウスで移動して設定します');
				gMap.setZoom(15);
			    if(document.getElementById(\"latitude\")) document.getElementById(\"latitude\").value = markers[0].marker.getPoint().y;
			    if(document.getElementById(\"longitude\")) document.getElementById(\"longitude\").value = markers[0].marker.getPoint().x;
				Markerable = false;
			}
	      }
	
	    }

	    function load() {
	      if (GBrowserIsCompatible()) {

		    var icon = G_DEFAULT_ICON;
	        var com01Icon = createIcon('com01');
	        var com02Icon = createIcon('com02');
	        var drg01Icon = createIcon('drg01');
	        var drg02Icon = createIcon('drg02');
	        var hsp01Icon = createIcon('hsp01');
	        var hsp02Icon = createIcon('hsp02');

	      	// setting
	      	draggableflg = {$default['draggableflg']};
	      	Markerable = {$default['Markerable']};
		    {$markerData}
			// end setting		


			// Initialize the map
	        gMap = new GMap2(document.getElementById(\"map\"));
			gMap.addControl(new GLargeMapControl());
			gMap.addControl(new GMapTypeControl());
			gMap.addControl(new GScaleControl());
			gMap.addControl(new GOverviewMapControl());
	        gMap.setCenter(new GLatLng({$default['lat']}, {$default['long']}), {$default['zoom']});
	        
            // Initialize the local searcher
			gLocalSearch = new GlocalSearch();
			gLocalSearch.setCenterPoint(gMap);
			gLocalSearch.setSearchCompleteCallback(null, OnLocalSearch);

			for (var i = 0; i < markers.length; i++) {
				var point = new GLatLng(markers[i].latitude,markers[i].longitude);
				var marker = createMarker(point,markers[i].icon)
				gMap.addOverlay(marker);
				markers[i].marker = marker;
			}
					
			GEvent.addListener(gMap, \"click\", function(marker, point) {
			  if (marker) {
			    marker.openInfoWindowHtml(createInnerHtml(marker));
			  } else {
			  	if(Markerable){
					gMap.addOverlay(createMarker(point,icon));
					Markerable = false;
				}
			  }
			});

	      }
	    }
	    
	    function showInfo(id){
			for (var i = 0; i < markers.length; i++) {
				if(markers[i].id==id){
					gMap.setCenter(new GLatLng(markers[i].latitude, markers[i].longitude), 13);
					markers[i].marker.openInfoWindowHtml(createInnerHtml(markers[i].marker));
				}
			}
	    }
	    //]]>
	    </script>
		";

		return $out;
	}

}
?>
